﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BestBuy.DAL;

namespace BestBuy
{
    public partial class GuestView : System.Web.UI.MasterPage
    {
        CCountry cCountry = new CCountry();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                cbCountry.DataSource = cCountry.GetList();
                cbCountry.DataTextField = "TenQuocGia";
                cbCountry.DataValueField = "IDQuocGia";
                cbCountry.DataBind();

                if (Request.Params["search"] != null)
                {
                    txtSearch.Text = Request.Params["search"].ToString();
                }
                if (Request.Params["type"] != null)
                {
                    cbSearchType.SelectedIndex = int.Parse(Request.Params["type"].ToString());
                }
                if (Request.Params["country"] != null)
                {
                    cbCountry.SelectedValue = Request.Params["country"].ToString();
                }
            }
        }
      
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (txtSearch.Text != "")
            {
                int SearchType = cbSearchType.SelectedIndex;
                string strSearch = txtSearch.Text;
                int country = int.Parse(cbCountry.SelectedValue);
                switch (SearchType)
                {
                    case 0: // Product
                        Response.Redirect("~/PageGuest/searchProducts.aspx?type=" + SearchType + "&search=" + strSearch + "&country=" + country);
                        break;
                    case 1: //Buy Offers
                        Response.Redirect("~/PageGuest/searchBuyOffers.aspx?type=" + SearchType + "&search=" + strSearch + "&country=" + country);
                        break;
                    case 2: // Sell Offers
                        Response.Redirect("~/PageGuest/searchSellOffers.aspx?type=" + SearchType + "&search=" + strSearch + "&country=" + country);
                        break;
                    case 3: // Companies
                        Response.Redirect("~/PageGuest/searchCompanies.aspx?type=" + SearchType + "&search=" + strSearch + "&country=" + country);
                        break;
                }
            }
        }

        protected void cbSearchType_SelectedIndexChanged(object sender, EventArgs e)
        {
            lbSearchType.Text = cbSearchType.SelectedItem.Text;
        }
    }
}